package com.glela.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.glela.common.constant.BaseConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author liujie
 */
public class DateUtils {
	// private static final long ONE_SECOND = 1;
	// private static final long ONE_MINUTE = 60;
	// private static final long ONE_HOUR = 3600;
	// private static final long ONE_DAY = 86400;
	// private static final long ONE_MONTH = 2592000;
	// private static final long ONE_YEAR = 31104000;

//	public static Calendar calendar = Calendar.getInstance();
	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

	private static String defaultDatePattern = "yyyy-MM-dd ";

	/**
	 * 在日期上增加天
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加天
	 */
	public static String addDay(Date date, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return format(cal.getTime(), pattern);
	}

	public static Date getDayStart(int n){
		Calendar c1 = Calendar.getInstance();
		c1.add(Calendar.DATE,n);
		c1.set(Calendar.HOUR_OF_DAY, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		return c1.getTime();
	}

	/**
	 * 在日期上增加数个整月
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 年/月/日类型的日期比较
	 * 
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 年/月/日 时:分类型的日期比较
	 * 
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compare_date_HHmm(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 年/月/日 时:分:秒 类型的日期比较
	 * 
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compare_date_HHmmss(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 日期格式化工具
	 * 
	 * @param date
	 *            需要格式化的日期
	 * @param format
	 *            需要的格式
	 * @return
	 */
	public static String dateFormat(Date date, String format) {
		SimpleDateFormat ft = new SimpleDateFormat(format);
		return ft.format(date).toString();
	}

	/**
	 * 字符类型日期格式化
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateFormatToString(String date, String format) {
		SimpleDateFormat ft = new SimpleDateFormat(format);
		return ft.format(date).toString();
	}

	/**
	 * 使用预设Format格式化Date成字符串
	 */
	public static String format(Date date) {
		return date == null ? " " : DateUtils.format(date,
				DateUtils.getDatePattern());
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 */
	public static String format(Date date, String pattern) {
		return date == null ? " " : new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 将yyyyMMdd格式的日期转为yyyy-MM-dd
	 * 
	 * @param str
	 * @return
	 */
	public static String formatDate(String str) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sfstr;
	}

	/**
	 * 距离截止日期还有多长时间
	 * 
	 * @param date
	 * @return
	 */
	public static long fromDeadline(Date date) {
		long deadline = date.getTime() / 1000;
		long now = new Date().getTime() / 1000;
		long remain = deadline - now;
		logger.info("剩下时间==" + remain);
		return remain;
		// if (remain <= ONE_SECOND) {
		// logger.debug("只剩下" + remain / ONE_MINUTE + "秒");
		// return remain / ONE_MINUTE + "";
		// }
		// else if (remain <= ONE_HOUR) {
		// logger.debug("只剩下" + remain / ONE_MINUTE + "分钟");
		// return remain / ONE_MINUTE + "";
		// }
		// else if (remain <= ONE_DAY) {
		// logger.debug("只剩下" + remain / ONE_HOUR + "小时" + remain % ONE_HOUR /
		// ONE_MINUTE + "分钟");
		// return remain / ONE_HOUR + "H" + remain % ONE_HOUR / ONE_MINUTE +
		// "S";
		// }
		// else {
		// long day = remain / ONE_DAY;
		// long hour = remain % ONE_DAY / ONE_HOUR;
		// long minute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE;
		// return "只剩下" + day + "天" + hour + "小时" + minute + "分钟";
		// }

	}

	/**
	 * 距离今天多久
	 * 
	 * @param date
	 * @return
	 */
	public static long fromToday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		long time = date.getTime() / 1000;
		long now = new Date().getTime() / 1000;
		long ago = now - time;
		logger.info("gao:" + ago);
		return ago;
	}

	public static String get24Hour() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.HOUR_OF_DAY) + "";
	}

	/**
	 * @return yyyy-mm-dd 2012-12-25
	 */
	public static String getDate() {
		return DateUtils.getYear() + "-" + DateUtils.getMonth() + "-"
				+ DateUtils.getDay();
	}

	/**
	 * @param format
	 * @return yyyy年MM月dd HH:mm MM-dd HH:mm 2012-12-25
	 */
	public static String getDate(String format) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simple = new SimpleDateFormat(format);
		return simple.format(calendar.getTime());
	}

	public static Date getDate(String year, String month, String day)
			throws ParseException {
		String result = year + "- "
				+ (month.length() == 1 ? "0 " + month : month) + "- "
				+ (day.length() == 1 ? "0 " + day : day);
		return DateUtils.parse(result);
	}

	/**
	 * @return yyyy-MM-dd HH:mm 2012-12-29 23:47
	 */
	public static String getDateAndMinute() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return simple.format(calendar.getTime());
	}

	/**
	 * 获得默认的 date pattern
	 */
	public static String getDatePattern() {
		return defaultDatePattern;
	}

	public static String getDay() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DATE) + "";
	}

	/**
	 * @return yyyy-MM-dd HH:mm:ss 2012-12-29 23:47:36
	 */
	public static String getFullDate() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simple.format(calendar.getTime());
	}

	/**
	 * 获取12小时制的当前小时
	 * 
	 * @return HH
	 */
	public static String getHour() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.HOUR);
		return month + "";
	}

	public static String getLastDayOfMonth(String year, String month) {
		Calendar cal = Calendar.getInstance();
		// 年
		cal.set(Calendar.YEAR, Integer.parseInt(year));
		// 月，因为Calendar里的月是从0开始，所以要-1
		// cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		// 日，设为一号
		cal.set(Calendar.DATE, 1);
		// 月份加一，得到下个月的一号
		cal.add(Calendar.MONTH, 1);
		// 下一个月减一为本月最后一天
		cal.add(Calendar.DATE, -1);
		return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
	}

	public static String getMinute() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MINUTE) + "";
	}

	public static String getMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		return month + "";
	}

	/**
	 * @return yyyyMMddHH:mm 201212292347
	 */
	public static String getRecordDate() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddHHmm");
		return simple.format(calendar.getTime());
	}

	public static String getSecond() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.SECOND) + "";
	}

	/**
	 * 返回预设Format的当前日期字符串
	 */
	public static String getToday() {
		Date today = new Date();
		return DateUtils.format(today);
	}

	public static String getYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR) + "";
	}

	public static void main(String[] args) throws ParseException {
		String deadline = "2015-11-22";
		String starline = "2015-01-13";
		// Date date = new Date();
		// SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
		// date = simple.parse(deadline);
		/*if (DateUtils.compare_date(DateUtils.getDate(), starline) >= 0
				&& DateUtils.compare_date(DateUtils.getDate(), deadline) <= 0) {
		}*/
		System.out.print(getDayStart(2).getTime());

		// String a = "2.5";
		// //这样
		// float b = new Float(a);
		// //或者这样
		// float c = Float.parseFloat(a);

		// String before = "2012-12-12 0:0:59";
		// date = simple.parse(before);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 */
	public static Date parse(String strDate) throws ParseException {
		return StringUtils.isBlank(strDate) ? null : DateUtils.parse(strDate,
				DateUtils.getDatePattern());
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parse(String strDate, String pattern)
			throws ParseException {
		return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
				pattern).parse(strDate);
	}

	/**
	 * 距离今天的绝对时间
	 * 
	 * @param date
	 * @return
	 */
	public static long toToday(Date date) {
		long time = date.getTime() / 1000;
		long now = new Date().getTime() / 1000;
		long ago = now - time;
		logger.info("距离今天的绝对时间==" + ago);
		return ago;
	}

	public static String getUTCByStr(String dateTime){
//		return dateTime.replace(" ","T")+"Z";
		Date time = DateUtil.parse(dateTime);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time.getTime());
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		String result = dateFormat(cal.getTime(), BaseConstants.DATE_FORMAT_UTC);
		return result;
	}

	public static String getUTCByDate(Date date){
		String time = DateUtil.toString(date);
		return getUTCByStr(time);
	}

}
